package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.CarManufacturerMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.CarManufacturer;
import com.wanghaixin.pojo.CarManufacturerExample;
import com.wanghaixin.service.CarmanuFacturerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 17:14
 * @Description:
 */
@Service
public class CarmanuFacturerServiceImpl implements CarmanuFacturerService {

    @Autowired
    private CarManufacturerMapper carManufacturerMapper;

    @Override
    public int addCarmanuFacturer(CarManufacturer carManufacturer) {
        int i = carManufacturerMapper.insertSelective(carManufacturer);
        return i;
    }

    @Override
    public int delCarmanuFacturer(Integer id) {
        int i = carManufacturerMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public int upCarmanuFacturer(CarManufacturer carManufacturer) {
        int update = carManufacturerMapper.updateByPrimaryKeySelective(carManufacturer);
        return update;
    }

    @Override
    public CarManufacturer findById(Integer id) {
        CarManufacturer carManufacturer = carManufacturerMapper.selectByPrimaryKey(id);
        return carManufacturer;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {

        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());

        CarManufacturerExample example = new CarManufacturerExample();
        if (queryDTO.getSort() != null && queryDTO.getSort().equals("")) {
            example.setOrderByClause("id");
        }
        List<CarManufacturer> carManufacturers = carManufacturerMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(carManufacturers);
        long total = pageInfo.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total,carManufacturers);
        return dataGridResult;
    }
}
